package com.i2bt.adapter;

import com.i2bt.common.UserConfig;

import com.i2bt.R;
import com.i2bt.app.friend.FriendMessageActivity;
import com.i2bt.bean.FriendBean;
import com.i2bt.util.FriendMessageUtil;
import com.i2bt.util.VisibilityUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class FriendMessageListAdapter extends BaseAdapter{
	
	private List<FriendBean> friendMessageData;
	
//	private static final String MESSAGE_REQUEST = "已发送";
//	
//	private static final String MESSAGE_ACCPET = "已接受";
//	
//	private static final String MESSAGE_REFUSE = "已拒绝";
	
	private FriendMessageActivity activity;

	public FriendMessageListAdapter(Context context, int resource,
			List<FriendBean> objects) {
		activity = (FriendMessageActivity) context;
		if (objects != null) {
			friendMessageData = objects;
		}
		else {
			friendMessageData = new ArrayList<FriendBean>();
		}
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(activity).inflate(
					R.layout.item_friend_message_list, null, false);
			
			holder = new ViewHolder();
			
			holder.friendImg =  (ImageView) convertView.findViewById(
					R.id.friend_message_item_img);
			holder.friendName = (TextView) convertView.findViewById(
					R.id.friend_message_item_name);
			holder.friendMessageContent = (TextView) convertView.findViewById(
					R.id.friend_message_item_content);
			holder.friendMessageText = (TextView) convertView.findViewById(
					R.id.friend_message_item_text);
			holder.friendMessageConcern = (Button) convertView.findViewById(
					R.id.friend_message_item_concern);
			
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.friendMessageConcern.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				activity.acceptFriendMessageRequest(position);
			}
		});
		
		FriendBean friendMessageBean = friendMessageData.get(position);
		//判断发送者是不是自己发的, 是的话就显示接受者, 不是的话就显示发送者
		if (friendMessageBean.getSender().equals(UserConfig.userName)) {
			holder.friendName.setText(friendMessageBean.getReceiver());
		}
		else {
			holder.friendName.setText(friendMessageBean.getSender());
		}
		holder.friendMessageContent.setText(friendMessageBean.getContent());
		
		//判断标志, 来改变消息列表项最后一个按钮和文本的切换显示
		switch (friendMessageBean.getFlag()) {
			
		//这个消息是请求的
		case FriendMessageUtil.REQUEST : {
			//判断这个消息是否是自己发送的, 自己发送就会隐藏关注按钮,显示出文本视图. 对方发的话就显示关注按钮.
			if (friendMessageBean.getSender().equals(UserConfig.userName)) {
				//显示文本视图
				VisibilityUtil.setVisibilityUI(holder.friendMessageConcern, false);
				VisibilityUtil.setVisibilityUI(holder.friendMessageText, true);
				holder.friendMessageText.setText("已发送");
				convertView.setClickable(false);
			}
			else {
				//显示关注按钮
				VisibilityUtil.setVisibilityUI(holder.friendMessageConcern, true);
				VisibilityUtil.setVisibilityUI(holder.friendMessageText, false);
				convertView.setClickable(true);
			}
			break;
		}
		
		//这个消息是接受
		case FriendMessageUtil.ACCEPT : {
			//隐藏关注按钮, 显示文本视图
			VisibilityUtil.setVisibilityUI(holder.friendMessageConcern, false);
			VisibilityUtil.setVisibilityUI(holder.friendMessageText, true);
			holder.friendMessageText.setText("已接受");
			convertView.setClickable(false);
			break;
		}
		
		//这个消息是拒绝
		case FriendMessageUtil.REFUSE : {
			//隐藏关注按钮, 显示文本视图
			VisibilityUtil.setVisibilityUI(holder.friendMessageConcern, false);
			VisibilityUtil.setVisibilityUI(holder.friendMessageText, true);
			if (friendMessageBean.getSender().equals(UserConfig.userName)) {
				holder.friendMessageText.setText("已拒绝");
			}
			else {
				holder.friendMessageText.setText("被拒绝");
			}
			convertView.setClickable(false);
			break;
		}
		
		}
		
		
		return convertView;
	}
	
	/**
	 * 
	 * Title : 内部视图持有对象
	 *
	 * Description : 包括了列表项需要的视图
	 *
	 * CreatedTime : 2014-8-22
	 *
	 * @author wuliao & kakaxi & yi
	 *
	 * @version 1.0
	 *
	 */
	private class ViewHolder {
		
		private ImageView friendImg;
		
		private TextView friendName;
		
		private TextView friendMessageContent;
		
		private Button friendMessageConcern;
		
		private TextView friendMessageText;
	}
	
	public void setData(List<FriendBean> friendMessageData) {
		this.friendMessageData = friendMessageData;
	}

	@Override
	public int getCount() {
		return friendMessageData.size();
	}

	@Override
	public Object getItem(int position) {
		return friendMessageData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}
